package com.pi.shisan.common;

//import com.shisan.pi.tools.bean.LedFontProperty;

import lombok.extern.slf4j.Slf4j;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
 * Created by Shisan on 2018/1/4.
 * 2019/12/20 update
 */
@Slf4j
public class TextPxCreater {

    private static Font simsunFont;


    public static int[] getAllPointsSameLine(String text, int fontSize) {
        return getAllPointsSameLine(text, fontSize, null);
    }

    /**
     *
     * @param text
     * @param fontSize
     * @param boundsInfo 可以使用该对象获取边缘信息
     * @return
     */
    public static int[] getAllPointsSameLine(String text, int fontSize, BoundsInfo boundsInfo) {
        if (simsunFont == null) {
            synchronized (TextPxCreater.class) {
                if (simsunFont == null) {
                    try {
                        simsunFont = Font.createFont(Font.TRUETYPE_FONT, TextPxCreater.class.getResourceAsStream("/simsun.ttc"));
                    } catch (Exception e) {
                        simsunFont = new Font("宋体",Font.PLAIN,12);
                        log.error("字体像素拾取器读取字体文件失败：",e);
                    }
                }
            }
        }
        int size = fontSize + 10;
        BufferedImage image = new BufferedImage(size,size,BufferedImage.TYPE_3BYTE_BGR);
        Graphics graphics = image.getGraphics();
        graphics.setFont(simsunFont.deriveFont(Font.PLAIN, fontSize));
        FontMetrics fontMetrics = graphics.getFontMetrics();
        char[] allText = text.toCharArray();
        int x = 0;
        int []allPoints = null;
        for (char c : allText) {
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0,0,size, size);
            graphics.setColor(Color.BLACK);
            //fontMetrics.getAscent() + fontMetrics.getDescent() 这玩意上了真机就不灵了，所以改自行测绘
//            graphics.drawString(String.valueOf(c),0, fontSize + 2);
            graphics.drawString(String.valueOf(c),0, fontMetrics.getAscent() + fontMetrics.getDescent() + fontMetrics.getLeading());

            int[] points = getPoint(image,x, 0);
            if (points != null) {
                if (allPoints == null) {
                    allPoints = points;
                } else {
                    int[] tmp = new int[allPoints.length + points.length];
                    System.arraycopy(allPoints,0,tmp,0,allPoints.length);
                    System.arraycopy(points,0,tmp,allPoints.length,points.length);
                    allPoints = tmp;
                }
            }
            Rectangle2D stringBounds = fontMetrics.getStringBounds(String.valueOf(c), graphics);
            x += stringBounds.getWidth() + 1;
        }
        int minY = Integer.MAX_VALUE;
        int maxY = 0;
        for (int i = 0; i < allPoints.length; i++) {
            int y = NumberUtil.toY(allPoints[i]);
            minY = Math.min(minY,y);
            maxY = Math.max(maxY, y);
        }
        if (minY > 0) {
            for (int i = 0; i < allPoints.length; i++) {
                allPoints[i] = NumberUtil.toXy(
                        NumberUtil.toX(allPoints[i]),
                        (short) (NumberUtil.toY(allPoints[i]) - minY)
                );
            }
        }
        if (boundsInfo != null) {
            boundsInfo.setMaxX(x - 1);
            boundsInfo.setMaxY(maxY - minY);
        }
        return allPoints;
    }


    public static int[] getPoint(BufferedImage image, int additionX, int additionY) {
        int size = 0;
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                int rgb = image.getRGB(x,y);
                if(rgb != -1){
                    size ++;
                }
            }
        }
        if (size > 0) {
            int[] points = new int[size];
            int index = 0;
            for (int y = 0; y < image.getHeight(); y++) {
                for (int x = 0; x < image.getWidth(); x++) {
                    int rgb = image.getRGB(x,y);
                    if(rgb != -1){
                        points[index++] = x + additionX << 16 | y + additionY;
                    }
                }
            }
            return points;
        }
        return null;
    }

    public static class BoundsInfo {
        private int maxX;
        private int maxY;

        public int getMaxX() {
            return maxX;
        }

        public void setMaxX(int maxX) {
            this.maxX = maxX;
        }

        public int getMaxY() {
            return maxY;
        }

        public void setMaxY(int maxY) {
            this.maxY = maxY;
        }
    }

}
